package cn.iocoder.boot.learn_200712_springmvc2.learn.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Boolean login(String userName, String password, HttpServletRequest request) {
        // 判空
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        // 判断账密正确与否
        if ("admin".equals(userName) && "admin".equals(password)) {
            // 将当前登录成功的用户名称存到 session 中，方便后面查找
            HttpSession session = request.getSession();
            session.setAttribute("userName", userName);
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/getLoginUser", method = RequestMethod.GET)
    public String getLoginUser (HttpSession session) {
        // 先判空
        if (session.getAttribute("userName") != null) {
            return (String) session.getAttribute("userName");
        }

        return "";
    }
}
